using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
using System.Xml;

namespace Umdf.SideShow.Scf.ObjectModel
{
    public class ScfMenuItem : ScfObject
    {
        private uint _id;
        private uint _targetId;
        private bool _isDefault;
        private bool _enabled = true;
        private uint _contextMenuId;
        private uint _imageId;
        private string _text = string.Empty;

        public uint Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public uint TargetId
        {
            get { return _targetId; }
            set { _targetId = value; }
        }

        public uint ImageId
        {
            get { return _imageId; }
            set { _imageId = value; }
        }

        public bool IsDefault
        {
            get { return _isDefault; }
            set { _isDefault = value; }
        }

        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        public uint ContextMenuId
        {
            get { return _contextMenuId; }
            set { _contextMenuId = value; }
        }

        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        protected internal override void DeserializeFromXml(XmlNode node)
        {
            base.DeserializeFromXml(node);
            _id = GetAttribute<uint>(node, "id");
            _targetId = GetAttribute<uint>(node, "target");
            _isDefault = GetAttribute<bool>(node, "def");
            _enabled = GetAttribute<bool>(node, "enable", true);
            _contextMenuId = GetAttribute<uint>(node, "menuid");
            _imageId = GetAttribute<uint>(node, "imgid");
            _text = string.Empty;
            foreach (XmlNode childNode in node.ChildNodes)
            {
                if (childNode.NodeType == XmlNodeType.Text)
                {
                    _text += childNode.Value;
                }
                else if(childNode.LocalName == "br")
                {
                    _text += "\r\n";
                }
            }
        }

        protected internal override void SerializeObject(System.IO.BinaryWriter writer)
        {
            base.SerializeObject(writer);
            writer.Write(_id);
            writer.Write(_targetId);
            writer.Write(_isDefault);
            writer.Write(_enabled);
            writer.Write(_contextMenuId);
            writer.Write(_imageId);
            writer.Write(Text);
        }

        protected internal override void DeserializeObject(System.IO.BinaryReader reader)
        {
            base.DeserializeObject(reader);
            _id = reader.ReadUInt32();
            _targetId = reader.ReadUInt32();
            _isDefault = reader.ReadBoolean();
            _enabled = reader.ReadBoolean();
            _contextMenuId = reader.ReadUInt32();
            _imageId = reader.ReadUInt32();
            _text = reader.ReadString();
        }
    }
}
